package com.easpeed.comon.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.easpeed.comon.bean.CommonKeyValue
import com.easpeed.elevator.R
import com.easpeed.elevator.utils.WwAdapter
import com.easpeed.elevator.utils.WwViewHolder

class PopwindowDropSelect(mContext: Context?, val isSingle: Boolean = true) : BasePopwindow(mContext) {
    fun showChoose(view: View?, defaultString: String?, width: Int = -1, opticalList: ArrayList<CommonKeyValue<String>>?, callback: (List<CommonKeyValue<String>>) -> Unit, result: (Int) -> Unit): WindowUI? {
        if (opticalList.isNullOrEmpty()) {
            return null
        }
        list.clear()
        result.invoke(1)
        val inflate: View = layoutInflater.inflate(R.layout.pop_show_drow_select, null)
        setContentView(inflate)
        setHeightWidth(-1, width)
        setAnimationStyle()
//        setBg(0.1f)
        if (isSingle) {
            opticalList.forEach {
                if (it.id == defaultString) {
                    it.isSelected = true
                    list.add(it)
                } else {
                    it.isSelected = false
                }
            }
        } else {
            if (!defaultString.isNullOrEmpty()) {

                opticalList.forEach {
                    if (it.id in defaultString.split("；")) {
                        it.isSelected = true
                        list.add(it)
                    } else {
                        it.isSelected = false
                    }
                }
            }
        }

        initOpticalView(inflate.findViewById(R.id.recylerview), opticalList) {
            callback.invoke(it)
        }
        ui.popupWindow.setOnDismissListener {
            result.invoke(0)
        }
        // 设置好参数之后再show
        ui.popupWindow.showAsDropDown(view)
        return ui
    }
    val list: ArrayList<CommonKeyValue<String>> = arrayListOf()


    private fun initOpticalView(recyclerView: RecyclerView, opticalList: ArrayList<CommonKeyValue<String>>?, callback: (List<CommonKeyValue<String>>) -> Unit) {
        var tempList: ArrayList<CommonKeyValue<String>>? = opticalList
        if (tempList == null) {
            tempList = arrayListOf()
        }
        var wwAdapterOptical: WwAdapter<CommonKeyValue<String>>? = null
        wwAdapterOptical = object : WwAdapter<CommonKeyValue<String>>() {

            override fun initRecyclerView(mContext: Context, adapter: WwAdapter<CommonKeyValue<String>>) {
                val manager = LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
                recyclerView.layoutManager = manager
                recyclerView.adapter = adapter
            }

            override fun getLayoutId(): Int {
                return R.layout.item_select
            }

            @SuppressLint("NotifyDataSetChanged")
            override fun convert(holder: WwViewHolder, bean: CommonKeyValue<String>, position: Int) {
                holder.setText(R.id.tv_line_name, bean.name)
                holder.setActivated(R.id.tv_line_name, bean.isSelected)
                holder.setOnClickListener(R.id.root, View.OnClickListener { view: View? ->
                    if (isSingle) {
                        list.clear()
                        bean.isSelected = !bean.isSelected
                        list.add(bean)
                        callback.invoke(list)
                        ui.popupWindow.dismiss()
                        wwAdapterOptical?.notifyDataSetChanged()
                    } else {

                        bean.isSelected = !bean.isSelected
                        if (bean.isSelected) {
                            list.add(bean)
                        } else {
                            list.remove(bean)
                        }
                        callback.invoke(list)
                        wwAdapterOptical?.notifyDataSetChanged()
                    }

                })

            }
        }.create(mContext, tempList)
    }

}
